<?php
if (! defined ( 'BASEPATH' )) {
	exit ( 'No direct script access allowed' );
}
/**
 * 年鉴模块操作
 * Enter description here ...
 * @author 庆安
 *
 */
class yearbook_model extends CI_Model {
	function __construct() {
		parent::__construct ();
	}
	/**
	 * 添加 年鉴
	 * Enter description here ...
	 * @param  $data Array
	 * 返回影响行数 0 失败 大于 0 成功
	 */
	function insertYearBook($data) {
		$this->db->insert ( 'wanwan_enum_yearbook', $data );
		return $this->db->insert_id ();
	}
	/**
	 * 更新 年鉴信息
	 * Enter description here ...
	 * @param  $data Array
	 * 返回 影响行数 0 失败 大于0 成功
	 */
	function updateUserYearBook($data) {
		$this->db->where ( "yearbookId", $data ['yearbookId'] );
		$this->db->update ( "wanwan_enum_yearbook", $this->__deleteKey ( 'yearbookId', $data ) );
		return $this->db->affected_rows ();
	}
	/**
	 * 获得年鉴列表
	 * Enter description here ...
	 * 返回结果集
	 */
	function queryYearBook() {
		$this->db->where ( 'deleFlag', 0);
		return $this->db->get ( 'wanwan_enum_yearbook' )->result_array ();
	}
	
	function queryPersonalYearbook($data,$limit,$offset){
		$limit--;
		$this->db->order_by("createTime", "desc");
		$data['deleFlag']=0;
		$rs=$this->db->get_where('wanwan_enum_yearbook',$data,  $offset,$limit);
		return $rs->result_array();
		
	}
	/**
	 * 如果发现查的是所有的，老大注意了，有问题 limit -1
	 * Enter description here ...
	 * @param unknown_type $teamId
	 * @param unknown_type $start
	 * @param unknown_type $pageSize
	 */
	function getTeamYearbook($teamId,$start=1,$pageSize=5){
		$start--;
		$sql = "SELECT * FROM wanwan_enum_yearbook  WHERE userid IN(SELECT userid FROM wanwan_enum_usertoteam WHERE teamId=$teamId)  ORDER BY createTime DESC    LIMIT  $start,$pageSize ";
		return $this->db->query ($sql)->result_array();
	
	}
	
	/**
	 * 删除年鉴 (伪操作)
	 * Enter description here ...
	 * @param  $data Array
	 * 返回影响行数 0 失败 大于 0 成功
	 */
	function deleteYearBook($data) {
		$this->db->where ( 'yearbookId', $data ['yearbookId'] );
		$updateData ['deleFlag'] = 1;
		$updateData ['deleTime'] = date ( 'Y-m-d H:i:s' );
		$this->db->update ( 'wanwan_enum_yearbook' );
		return $this->db->affected_rows ();
	}
	
	function addYearbook($data) {
		$errorMsg = "年鉴添加成功";
		$successMsg = "";
		$data ['createTime'] = date ( 'Y-m-d H:i:s' );
		$int= $this->insertYearBook ( $data ) ;
		if (empty ($int)) {
			$successMsg = "年鉴添加失败";
		}
		return array ('msg' => $successMsg . $errorMsg . "." );
	}
}
?>